<?php
/**
 * --------------------------------------------------------------
 * 生成多层树状结构
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace util;

class Tree
{
    /**
     * 将数据集转换成树状结构
     * @param array $list 要转换的数据集
     * @param string $pid 父元素标记字段
     * @param string $level 层级标记字段
     * @return array
     */
    public static function toTree($list = null, $pk ='id', $pid = 'pid', $child = '_child')
    {
        if (null === $list) $list = & $this->dataList; // 默认直接返回查询结果集合

        $tree = [];
        if (is_array($list)) {
            $refer = []; // 创建基于主键的数据引用
            foreach ($list as $key => $data) {
                $_key         = is_object($data) ? $data->$pk : $data[$pk];
                $refer[$_key] = & $list[$key];
            }
            foreach ($list as $key => $data) {
                $parentId     = is_object($data) ? $data->$pid : $data[$pid];
                $is_exist_pid = false;
                foreach ($refer as $k => $v) {
                    if ($parentId == $k) {
                        $is_exist_pid = true;
                        break;
                    }
                }
                if ($is_exist_pid) {
                    if (isset($refer[$parentId])) {
                        $parent           = & $refer[$parentId];
                        $parent[$child][] = & $list[$key];
                    }
                } else {
                    $tree[] = & $list[$key];
                }
            }
        }

        return $tree;
    }

    /**
     * 转换数组结构
     * @param array $list
     * @param integer $level 进行递归时传递用的参数
     * @return array
     */
    private static $formatTree;
    private static function _toFormatTree($list, $level = 0, $title = 'title')
    {
        foreach ($list as $key => $val) {
            $temp_str          = str_repeat('&nbsp;', $level * 2);
            $temp_str         .= '┗';
            $val['level']      = $level;
            $val['title_show'] = $level == 0 ? $val[$title] . '&nbsp;' : $temp_str . $val[$title] . '&nbsp;';
            if (!array_key_exists('_child', $val)) {
                array_push(self::$formatTree, $val);
            } else {
                $temp_arr = $val['_child'];
                unset($val['_child']);
                array_push(self::$formatTree, $val);
                self::_toFormatTree($temp_arr, $level + 1, $title);
            }
        }

        return;
    }

    public static function toFormatTree($list, $title = 'title', $pk = 'id', $pid = 'pid', $root = 0)
    {
        $list = \list_to_tree($list, $pk, $pid, '_child', $root);
        self::$formatTree = [];
        self::_toFormatTree($list, 0, $title);
        return self::$formatTree;
    }

    /**
     * 根据子节点返回所有父节点
     * @param array $list 数据集
     * @param string $id 子节点id
     * @return array
     */
    public static function getParents($list = [], $id = '')
    {
        $tree = [];
        foreach ($list as $value) {
            if ($value['id'] == $id) {
                $tree[] = $value;
                $tree   = array_merge(self::getParents($list, $value['pid']), $tree);
            }
        }

        return $tree;
    }
}